#include "test.h"
#include "2d\CCNode.h"
#include "TiledMap.h"
#include "TiledParser.h"
#include "base\CCDirector.h"
#include "TiledLayer.h"
#include "CCFileUtils.h"
#include "2d\CCSpriteFrame.h"

using namespace Tiled;

void testlayer::startTest()
{
	auto director = Director::getInstance();

	FileUtils::getInstance()->addSearchPath("res/");
	Scene* sc = Scene::create();
	director->runWithScene(sc);
	Tiled::TmxMap* tmx = Tiled::TmxMap::create("TileMaps/tile_iso_offset.tmx");
	sc->addChild(tmx);

	director->setDisplayStats(true);

	testlayer* t = new testlayer;
	t->autorelease();
	t->tilemap = tmx;
	t->setContentSize(director->getWinSize());
	t->setTouchEnabled(true);
	sc->addChild(t);
}

void testlayer::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
{
	auto touch = touches[0];

	auto diff = touch->getDelta();
	auto currentPos = tilemap->getPosition();
	tilemap->setPosition(currentPos + diff);
}

void testlayer::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event)
{
	auto touch = touches[0];
	TmxMap* tmx = (TmxMap*)tilemap;
	
	Vec2 idx = tmx->getInfo()->posToTileIndex(touch->getLocation().x, touch->getLocation().y);

	CCLOG("%d %d", (int)idx.x, (int)idx.y);
}

